//
//  ReplyCell.m
//  Azkar
//
//  Created by 王易 on 2018/8/27.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "ReplyCell.h"

/**
 回复Cell
 */
@interface ReplyCell()

@property (nonatomic,weak) UIImageView *iconV;

@property (nonatomic,weak) UILabel *userName;

@property (nonatomic,weak) UILabel *desc;

@property (nonatomic,weak) UILabel *likeCount;

@property (nonatomic,weak) MyRelativeLayout *likeLayout;

@property (nonatomic,weak) UIImageView *likeImage;

@property (nonatomic,weak) UILabel *message;
@end
@implementation ReplyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        MyRelativeLayout *rootLayout = [MyRelativeLayout new];
        rootLayout.backgroundColor = [UIColor whiteColor];
        rootLayout.wrapContentHeight = YES;
        rootLayout.cacheEstimatedRect = YES;
        rootLayout.myHorzMargin = 0;
        rootLayout.padding = UIEdgeInsetsMake(12, 12, 12, 12);
        self.rootLayout = rootLayout;
        [self.contentView addSubview:rootLayout];

        [self addLikeLayout:rootLayout];
        
        [self addMoreMessage:rootLayout];
        
        
        MyBorderline  *bld2 = [[MyBorderline alloc] initWithColor:Color(@"#EDEDED")];
        bld2.headIndent = bld2.tailIndent = 20;
        rootLayout.bottomBorderline = bld2;
    }
    return self;
}

- (void)addLikeLayout:(MyBaseLayout *)superLayout
{
    MyRelativeLayout *likeLayout = [MyRelativeLayout new];
    self.likeLayout = likeLayout;
    likeLayout.wrapContentSize = YES;
    likeLayout.padding = UIEdgeInsetsMake(5, 5, 5, 5);
    likeLayout.myTrailing = 0;
    likeLayout.myTop = 2;
    [superLayout addSubview:likeLayout];
    
    
    
    UIImageView *imageV = [UIImageView new];
    self.likeImage = imageV;
    imageV.image = Image(@"like_ic");
    imageV.myWidth = 17;
    imageV.myHeight = 18;
    imageV.myLeading = 0;
    imageV.myTop = 0;
    [likeLayout addSubview:imageV];
    
    UILabel *likeCount = [UILabel new];
    self.likeCount = likeCount;
    likeCount.textColor = Color(@"#6F6F6F");
    likeCount.font = FontSize(13);
    likeCount.leadingPos.equalTo(imageV.trailingPos).offset(6);
    likeCount.myTop = 1;
    likeCount.wrapContentSize = YES;
    [likeLayout addSubview:likeCount];
    WeakSelf
    [likeLayout hyb_addTapGestureWithCallback:^(UITapGestureRecognizer *sender) {
        StrongSelf
        if (self.likeBlock) {
            self.likeBlock(self.commentsModel);
        }
    }];
}
- (void)addMoreMessage:(MyBaseLayout *)baseLayout
{
    MyRelativeLayout *relativeLayout = [MyRelativeLayout new];
    relativeLayout.myLeading = 0;
    relativeLayout.trailingPos.equalTo(self.likeLayout.leadingPos).offset(10);
    relativeLayout.myTop = 0;
    relativeLayout.wrapContentHeight = YES;
    [baseLayout addSubview:relativeLayout];
    
    UIImageView *iconV = [UIImageView new];
    self.iconV = iconV;
    iconV.layer.cornerRadius = 17;
    iconV.layer.masksToBounds = YES;
    iconV.myLeading = iconV.myTop = 0;
    iconV.myWidth = iconV.myHeight = 34;
    [relativeLayout addSubview:iconV];
    
    UILabel *userName = [UILabel new];
    self.userName = userName;
    userName.wrapContentHeight = YES;
    userName.numberOfLines = 1;
    userName.leadingPos.equalTo(iconV.trailingPos).offset(8);
    userName.myTrailing = 0;
    userName.textColor = Color(@"#525252");
    userName.font = FontSize(13);
    userName.myTop = 0;
    [relativeLayout addSubview:userName];
    
    UILabel *desc = [UILabel new];
    self.desc = desc;
    desc.leadingPos.equalTo(userName.leadingPos);
    desc.myTrailing = 0;
    desc.topPos.equalTo(userName.bottomPos).offset(3);
    desc.font = FontSize(11);
    desc.textColor = Color(@"#929292");
    desc.wrapContentHeight = YES;
    [relativeLayout addSubview:desc];
    
    UILabel *message = [UILabel new];
    self.message = message;
    message.topPos.equalTo(relativeLayout.bottomPos).offset(15);
    message.myLeading = 42;
    message.myTrailing = 0;
    message.wrapContentHeight = YES;
    message.textColor = Color(@"#525252");
    message.font = FontSize(15);
    [baseLayout addSubview:message];
    
}
- (void)setCommentsModel:(ReplyListModel *)commentsModel
{
    _commentsModel = commentsModel;
    
    if (commentsModel.like) {
        self.likeImage.image = Image(@"like_ic_s");
        self.likeCount.textColor = Color(@"#FF7878");
    }else{
        self.likeImage.image = Image(@"like_ic");
        self.likeCount.textColor = Color(@"#6F6F6F");
    }
    
    self.likeCount.text = commentsModel.cnt_like;
    [self.likeCount sizeToFit];
    
    [self.iconV sd_setImageWithURL:[NSURL URLWithString:commentsModel.avatar] placeholderImage:Image(@"no_photo")];
    self.userName.text = commentsModel.commenter_name;
    self.userName.textAlignment = [MyBaseLayout isRTL] ? NSTextAlignmentRight : NSTextAlignmentLeft;
    [self.userName sizeToFit];
    
    NSString *time = [DateHelper dateStrFromTimeStamp:commentsModel.comment_time dateFormat:@"dd/MM HH:mm"];
    self.desc.text = time;
    
    [self.desc sizeToFit];
    
    self.message.text = commentsModel.content;
    self.message.textAlignment = [MyBaseLayout isRTL] ? NSTextAlignmentRight : NSTextAlignmentLeft;
    [self.message sizeToFit];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
